package com.ikkong.platform.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ikkong.core.base.BaseController;
import com.ikkong.core.dao.Blade;
import com.ikkong.core.dao.Db;
import com.ikkong.core.jfinal.ext.kit.JsonKit;
import com.ikkong.core.toolbox.Record;
import com.ikkong.system.controller.base.UrlPermissController;
import com.ikkong.platform.meta.intercept.UserDocumentIntercept;
import com.ikkong.platform.model.Template;
import com.ikkong.platform.model.TemplateAttr;
import com.ikkong.platform.model.UserDocument;
import com.ikkong.platform.model.UserInfo;
import com.ikkong.platform.service.UserDocumentService;
import com.ikkong.platform.service.UserInfoService;
import com.ikkong.platform.service.impl.UserDocumentServiceImpl;
import com.ikkong.platform.service.impl.UserInfoServiceImpl;

/**
 * Generated by Blade. 2018-06-26 13:36:25
 */
public class UserDocumentController extends UrlPermissController {
	private static String CODE = "userDocument";
	private static String PERFIX = "tb_user_document";
	private static String LIST_SOURCE = "UserDocument.list";
	private static String BASE_PATH = "/platform/userDocument/";

	UserDocumentService service = new UserDocumentServiceImpl();
	UserInfoService userInfoService = new UserInfoServiceImpl();

	public void index() {
		setAttr("code", CODE);
		render(BASE_PATH + "userDocument.html");
	}

	public void add() {
		setAttr("code", CODE);
		render(BASE_PATH + "userDocument_add.html");
	}

	public void edit() {
		String id = getPara(0);
		UserDocument userDocument = service.findById(id);
		setAttr("model", JsonKit.toJson(userDocument));
		setAttr("id", id);
		setAttr("code", CODE);
		render(BASE_PATH + "userDocument_edit.html");
	}

	public void view() {
		String id = getPara(0);
		UserDocument userDocument = service.findById(id);
		String content = userDocument.getMark();
		int user_id = userDocument.getId();
		Db dao = Db.init();
		List<Record> userInfos = dao
				.selectList(
						"select u.*,a.ename,a.cname from tb_user_info u left join (select id,ename,cname,'order',isMust from tb_template_attr ) a "
								+ "on u.template_attr_id=a.id where u.user_id=#{user_id}",
						Record.create().set("user_id", user_id));

		Map<String, String> mapstring = new HashMap<>();
		for (Record r : userInfos) {
			mapstring.put(r.getStr("ename"), r.getStr("value"));
		}

		for (Map.Entry<String, String> entry : mapstring.entrySet()) {
			content = content.replace("${" + entry.getKey() + "}",
					entry.getValue());
		}

		userDocument.setMark(content);
		setAttr("model", JsonKit.toJson(userDocument));
		setAttr("id", id);
		setAttr("code", CODE);
		render(BASE_PATH + "userDocument_view.html");
	}

	public void list() {
		Object grid = paginate(LIST_SOURCE,new UserDocumentIntercept());
		renderJson(grid);
	}

	public void save() {
		UserDocument userDocument = mapping(PERFIX, UserDocument.class);
		boolean temp = service.save(userDocument);
		if (temp) {
			renderJson(success(SAVE_SUCCESS_MSG));
		} else {
			renderJson(error(SAVE_FAIL_MSG));
		}
	}

	public void update() {
		UserDocument userDocument = mapping(PERFIX, UserDocument.class);
		// userDocument.setVersion(userDocument.getVersion()+1);
		boolean temp = service.update(userDocument);
		if (temp) {
			renderJson(success(UPDATE_SUCCESS_MSG));
		} else {
			renderJson(error(UPDATE_FAIL_MSG));
		}
	}

	public void remove() {
		String ids = getPara("ids");
		int cnt = service.deleteByIds(ids);
		if (cnt > 0) {
			renderJson(success(DEL_SUCCESS_MSG));
		} else {
			renderJson(error(DEL_FAIL_MSG));
		}
	}
}
